package cn.xsly.pet.service.impl;

import cn.xsly.basic.service.IBaiduAiAuditService;
import cn.xsly.basic.util.AjaxResult;
import cn.xsly.pet.domain.Pet;
import cn.xsly.pet.domain.PetDetail;
import cn.xsly.pet.domain.PetOnlineAuditLog;
import cn.xsly.pet.mapper.PetDetailMapper;
import cn.xsly.pet.mapper.PetMapper;
import cn.xsly.pet.mapper.PetOnlineAuditLogMapper;
import cn.xsly.pet.service.IPetService;
import cn.xsly.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private IBaiduAiAuditService iBaiduAiAuditService;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    //宠物进行添加在把明细添加进去
    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            //添加petid为空，需要设置进去
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetailMapper.update(petDetail);
        }
    }

    @Transactional
    @Override
    public void delete(Long id) {
        super.delete(id);
        petDetailMapper.deleteByPetId(id);
    }
    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        //获取要上假的id
        //根据id去查该宠物的文本和图片资源，需要做审核
        //如果都通过，日志里面把该宠物的state设为1，失败也添加审核日志
        for(Long id:ids){
            Pet pet = petMapper.findById(id);
            Boolean textAudit = iBaiduAiAuditService.textAudit(pet.getName());
            Boolean imageAudit = iBaiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            if (textAudit&&imageAudit){
                petMapper.onsale(id,new Date());
                log.setState(1);
                log.setNote("审核通过");
                petOnlineAuditLogMapper.add(log);
            }else {
                log.setState(0);
                log.setNote("审核失败");
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMessage("上架失败");
            }
        }
        return new AjaxResult();
    }

    @Override
    public AjaxResult offsale(List<Long> ids) {
        petMapper.offsale(ids,new Date());
        return new AjaxResult();
    }
}